package servlet.news;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jdbc.DBUtils;
import model.NewsChannel;

@WebServlet("/NewspreAddServlet")
public class NewspreAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Connection connection = DBUtils.getConn();
		
		try {
			PreparedStatement ps = connection.prepareStatement("select * from news_channel");
			ResultSet rs = ps.executeQuery();
			
			 List<NewsChannel> newsChannellist = new ArrayList<>();
			 while (rs.next()) {
				 NewsChannel newsChannel = new NewsChannel();
				 newsChannel.setId(rs.getInt("id"));
				 newsChannel.setName(rs.getString("name"));
				 newsChannellist.add(newsChannel);
			}
			 request.setAttribute("newsChannellist",newsChannellist);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		 //转发请求到另一个servlet url 不变
		request.getRequestDispatcher("add.jsp").forward(request,response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


